<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>科乐云自主开发AI客户端</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }
        
        .chat-container {
            width: 100%;
            max-width: 800px;
            height: 90vh;
            background-color: white;
            border-radius: 16px;
            box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        
        .chat-header {
            background-color: #007bff;
            padding: 20px;
            color: white;
            display: flex;
            align-items: center;
        }
        
        .chat-title {
            font-size: 18px;
            font-weight: 600;
        }
        
        .chat-messages {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            background-color: #f8f9fa;
        }
        
        .message {
            margin-bottom: 20px;
            display: flex;
            flex-direction: column;
        }
        
        .message-header {
            display: flex;
            align-items: center;
            margin-bottom: 5px;
        }
        
        .avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            margin-right: 10px;
            object-fit: cover;
        }
        
        .message-name {
            font-size: 12px;
            font-weight: 600;
            margin-bottom: 3px;
        }
        
        .ai-name {
            color: #007bff;
        }
        
        .user-name {
            color: #28a745;
        }
        
        .message-content {
            max-width: 80%;
            padding: 12px 16px;
            border-radius: 8px;
            word-wrap: break-word;
            position: relative;
            animation: fadeIn 0.3s ease;
        }
        
        .ai-message .message-content {
            background-color: white;
            align-self: flex-start;
            border: 1px solid #eaeaea;
        }
        
        .user-message .message-content {
            background-color: #e3f2fd;
            align-self: flex-end;
            margin-left: auto;
        }
        
        .chat-input {
            background-color: #f0f2f5;
            padding: 20px;
            display: flex;
            align-items: center;
        }
        
        .input-field {
            flex: 1;
            padding: 12px 16px;
            border: 1px solid #eaeaea;
            border-radius: 8px;
            font-size: 16px;
            outline: none;
            transition: border-color 0.3s;
        }
        
        .input-field:focus {
            border-color: #007bff;
        }
        
        .send-button {
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 8px;
            padding: 12px 20px;
            margin-left: 10px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        .send-button:hover {
            background-color: #0056b3;
        }
        
        .send-button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 2px solid #f3f3f3;
            border-top: 2px solid #007bff;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-left: 10px;
            vertical-align: middle;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        @media (max-width: 768px) {
            .chat-container {
                height: 100vh;
                border-radius: 0;
            }
            
            .message-content {
                max-width: 90%;
            }
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">
            <div class="chat-title">AI</div>
        </div>
        <div class="chat-messages" id="chat-messages">
            <div class="message ai-message">
                <div class="message-header">
                    <img src="https://via.placeholder.com/36/007bff/ffffff?text=AI" class="avatar">
                    <div class="message-name ai-name">AI助手</div>
                </div>
                <div class="message-content">
                    你好！我是AI助手，很高兴为你服务。有什么我可以帮你的吗？
                </div>
            </div>
        </div>
        <div class="chat-input">
            <input type="text" class="input-field" id="user-input" placeholder="输入你的问题...">
            <button class="send-button" id="send-button">发送</button>
        </div>
    </div>

    <script>
        const chatMessages = document.getElementById('chat-messages');
        const userInput = document.getElementById('user-input');
        const sendButton = document.getElementById('send-button');
        
        // 添加消息
        function addMessage(text, isUser) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : 'ai-message'}`;
            
            // 消息头部
            const headerDiv = document.createElement('div');
            headerDiv.className = 'message-header';
            
            // 头像
            const avatar = document.createElement('img');
            avatar.className = 'avatar';
            avatar.src = isUser 
                ? 'https://via.placeholder.com/36/28a745/ffffff?text=用户' 
                : 'https://via.placeholder.com/36/007bff/ffffff?text=AI';
            headerDiv.appendChild(avatar);
            
            // 名称
            const nameDiv = document.createElement('div');
            nameDiv.className = `message-name ${isUser ? 'user-name' : 'ai-name'}`;
            nameDiv.textContent = isUser ? '用户' : 'AI助手';
            headerDiv.appendChild(nameDiv);
            
            // 消息内容
            const contentDiv = document.createElement('div');
            contentDiv.className = 'message-content';
            contentDiv.textContent = text;
            
            // 组合
            messageDiv.appendChild(headerDiv);
            messageDiv.appendChild(contentDiv);
            chatMessages.appendChild(messageDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }
        
        // 加载动画
        function toggleLoading(show) {
            if (show) {
                const loadingDiv = document.createElement('div');
                loadingDiv.className = 'message ai-message';
                
                const headerDiv = document.createElement('div');
                headerDiv.className = 'message-header';
                
                const avatar = document.createElement('img');
                avatar.className = 'avatar';
                avatar.src = 'https://via.placeholder.com/36/007bff/ffffff?text=AI';
                headerDiv.appendChild(avatar);
                
                const nameDiv = document.createElement('div');
                nameDiv.className = 'message-name ai-name';
                nameDiv.textContent = 'AI助手';
                headerDiv.appendChild(nameDiv);
                
                loadingDiv.appendChild(headerDiv);
                
                const contentDiv = document.createElement('div');
                contentDiv.className = 'message-content';
                contentDiv.innerHTML = '正在思考中<span class="loading"></span>';
                loadingDiv.appendChild(contentDiv);
                
                chatMessages.appendChild(loadingDiv);
                chatMessages.scrollTop = chatMessages.scrollHeight;
                return loadingDiv;
            } else {
                const loadingElements = document.querySelectorAll('.ai-message:has(.loading)');
                if (loadingElements.length > 0) {
                    loadingElements[0].remove();
                }
            }
        }
        //版权信息科乐云所有https://keleyuncloud.dpdns.org
        // 发送消息
        async function sendMessage() {
            const message = userInput.value.trim();
            if (!message) return;
            
            sendButton.disabled = true;
            userInput.value = '';
            
            addMessage(message, true);
            const loadingElement = toggleLoading(true);
            
            try {
                const response = await fetch('api.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ message }),
                });
                
                const data = await response.json();
                
                toggleLoading(false);
                
                if (data.error) {
                    addMessage(`错误: ${data.error}`, false);
                } else if (data.answer) {
                    addMessage(data.answer, false);
                }
            } catch (error) {
                toggleLoading(false);
                addMessage(`网络请求失败: ${error.message}`, false);
            }
            
            sendButton.disabled = false;
            userInput.focus();
        }
        
        sendButton.addEventListener('click', sendMessage);
        userInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                sendMessage();
            }
        });
        
        userInput.focus();
    </script>
</body>
</html>